package com.lantosec.hmrp.modules.supplier.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.config.InitDataListener;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.supplier.entity.SupplierLinkman;
import com.lantosec.hmrp.modules.supplier.service.ISupplierLinkmanService;
import com.lantosec.hmrp.modules.supplier.vo.SupplierLinkmanVO;
import com.lantosec.hmrp.modules.uploads.service.ISysUploadsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 供应商联系人 控制器
 *
 * @author AutoCode
 * @since 2020-02-20
 */
@Api(value = "供应商联系人控制器", tags = "供应商联系人控制器")
@RestController
@RequestMapping("/supplier/linkman")
@Log4j2
public class SupplierLinkmanController extends BaseController {
    @Autowired
    private ISupplierLinkmanService service;

    @Autowired
    private ISysUploadsService iSysUploadsService;

    @GetMapping("queryAll")
    @ApiOperation(value = "查询全部")
    public Result<List<SupplierLinkman>> queryAll(@ApiParam(value = "供应商编码") @RequestParam(name = "supplierCode")String supplierCode){

        return Result.ok(service.queryAll(supplierCode));
    }

    @GetMapping("querySupplierLinkman")
    @ApiOperation(value = "供应商联系人查询")
    public Result<PageVO<SupplierLinkmanVO>> querySupplierLinkman(@ApiParam(value = "分页对象", required = true) PageVO<SupplierLinkman> page,
                                                                  @ApiParam(value = "供应商编码") @RequestParam(name = "supplierCode",required = false) String supplierCode,
                                                                  @ApiParam(value = "供应商联系人姓名") @RequestParam(name = "linkmanName",required = false) String linkmanName,
                                                                  @ApiParam(value = "供应商联系人手机号") @RequestParam(name = "linkmanTel",required = false) String linkmanTel,
                                                                  @ApiParam(value = "是否是业务员") @RequestParam(name = "salesmanFlag",required = false) Integer salesmanFlag){
        return Result.ok(service.querySupplierLinkman(page,supplierCode,linkmanName,linkmanTel,salesmanFlag));
    }

    @GetMapping("querySupplierLinkmanById/{supplierCode}")
    @ApiOperation(value = "查询供应商业务员的信息")
    public Result<Page<SupplierLinkmanVO>> querySupplierLinkmanById(@ApiParam(value = "分页对象", required = true) Page<SupplierLinkman> page,
                                                    @ApiParam(value = "supplierCode", required = true) @PathVariable(name="supplierCode") String supplierCode){
        return Result.ok(service.getBySupplierCode(page,supplierCode));
    }

    @PostMapping("insertSupplierLinkmanInfo")
    @ApiOperation(value = "新增供应商业务员的信息")
    public Result<String> insertSupplierLinkmanInfo(@ApiParam(value = "供应商联系人VO对象", required = true) @RequestBody @Validated SupplierLinkmanVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        SupplierLinkman entity = vo.convertToEntity();
        entity.setLinkmanId(UUIDUtils.get());
        service.save(entity);
        return Result.ok(entity.getLinkmanId());
    }

    @GetMapping("getSaveUrl/{linkmanId}")
    @ApiOperation(value = "编辑供应商联系人时附件所存放的路径")
    public Result<SupplierLinkmanVO> getSaveUrl(@ApiParam(value = "业务员id", required = true) @PathVariable(name = "linkmanId") String linkmanId){
        SupplierLinkmanVO supplierLinkmanVO = service.getSavePath(linkmanId);
        return Result.ok(supplierLinkmanVO);
    }

    @GetMapping("getOneUrl/{id}")
    @ApiOperation(value = "身份证正面或者购销资格证附件所存放的路径")
    public Result<Map<String,String>> getOneUrl(@ApiParam(value = "id", required = true) @PathVariable(name = "id") String id){
        String parm = iSysUploadsService.getById(id).getSavePath();
        String fileName = iSysUploadsService.getById(id).getFileName();
        Map<String,String> map = new HashMap();
        map.put("id", InitDataListener.uploadsUrl +parm+fileName);
        return Result.ok(map);
    }


    @PutMapping("updateSupplierLinkmanInfo")
    @ApiOperation(value = "修改数据")
    public Result<Void> updateSupplierLinkmanInfo(@ApiParam(value = "供应商联系人VO对象", required = true) @RequestBody SupplierLinkmanVO vo){
        SupplierLinkman entity = vo.convertToEntity();
        Boolean parms = service.updateById(entity);
        if(parms){
            return Result.ok();
        }else{
            return Result.fail("操作失败");
        }

    }

    @DeleteMapping(value = "deleteSupplierLinkmanInfo/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> deleteSupplierLinkmanInfo(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }


}
